const mongoose = require('mongoose');
const Resource = require('../models/Resource');
require('dotenv').config();

const sampleActivities = [
  {
    title: '社区植树活动',
    description: '参与社区植树活动，为环境贡献一份力量。每个参与者将种植至少一棵树，并负责后续的养护工作。',
    type: '活动',
    status: 'active',
    startDate: new Date(),
    endDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
    location: '社区公园',
    checkInRequired: 5,
    points: 100,
    imageUrl: '/images/activities/tree-planting.jpg',
    participants: []
  },
  {
    title: '垃圾分类挑战',
    description: '连续30天正确进行垃圾分类，培养环保习惯。参与者需要每天拍照记录自己的垃圾分类情况。',
    type: '活动',
    status: 'active',
    startDate: new Date(),
    endDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
    location: '线上活动',
    checkInRequired: 30,
    points: 200,
    imageUrl: '/images/activities/waste-sorting.jpg',
    participants: []
  },
  {
    title: '低碳出行周',
    description: '一周内使用公共交通、自行车或步行代替私家车出行，减少碳排放。',
    type: '活动',
    status: 'active',
    startDate: new Date(),
    endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
    location: '全市范围',
    checkInRequired: 7,
    points: 150,
    imageUrl: '/images/activities/low-carbon-travel.jpg',
    participants: []
  }
];

async function seedActivities() {
  try {
    // 连接数据库
    await mongoose.connect(process.env.MONGODB_URI);
    console.log('数据库连接成功');
    
    // 检查是否已有活动
    const existingCount = await Resource.countDocuments({ type: '活动' });
    if (existingCount > 0) {
      console.log(`数据库中已有 ${existingCount} 个活动，跳过添加`);
      mongoose.connection.close();
      return;
    }
    
    // 添加示例活动
    await Resource.insertMany(sampleActivities);
    console.log(`成功添加 ${sampleActivities.length} 个示例活动`);
    
    mongoose.connection.close();
  } catch (error) {
    console.error('添加示例活动失败:', error);
    mongoose.connection.close();
  }
}

seedActivities(); 